<template>
    <div>
     <header>
         <i @click="back" class="iconfont">&#xe663;</i>
         <p>地址列表</p>
         
     </header>
      <van-address-list
        v-model="chosenAddressId"
        :list="list"
        :disabled-list="disabledList"
        disabled-text="以下地址超出配送范围"
        default-tag-text="默认"
        switchable
        @add="onAdd"
        @edit="onEdit"
        @select="onSelect"
        />
    </div>
</template>

<script>
import {Toast} from 'vant'
import {ajax_getAddress} from '../ajax/index'
import {mapState} from 'vuex'
export default {
   data() {
    return {
      chosenAddressId: '1',
      list: [],
      disabledList: [
        {
          id: '3',
          name: '王五',
          tel: '1320000000',
          address: '浙江省杭州市滨江区江南大道 15 号',
        },
      ],
    };
  },
  computed:{
     ...mapState(['users'])
  },
  methods: {
    onAdd() {
      Toast('新增地址');
      this.$router.push('/addDzhi')
      
    },
    onEdit(item, index) {
      Toast('编辑地址:' + index);
    },
    onSelect(item,index){
          console.log(item,index) 
    },
    back(){
        this.$router.push('/ziLiao')
    }
  },
  created(){
    ajax_getAddress({
      user_Id:this.users.Id
    }).then(res=>{
      console.log(res.data1);
      res.data1.reverse().forEach((item,i) => {
        var obj={};
        obj.id=`${i+1}`;
        obj.Id=item.Id;
        obj.name=item.username;
        obj.tel=item.phone;
        obj.address=item.address;
        if(item.isTrue_mo){
             obj.isDefault=item.isTrue_mo==1?true:false;
        }
        this.list.push(obj)
      });
      // this.list=res.data1
      var obj={}
      this.list.forEach((item,i,array) => {
        if(item.isDefault){
          obj=item
          array.splice(i,1);
        }
      });
      this.list.unshift(obj)
      this.list.forEach((item,i,array)=>{
        item.id=`${i+1}`
      })
      console.log(this.list)
    })
  }
}
</script>

<style scoped>
header{
    position: relative;
    width: 100vw;
    padding: 2vh 0;
    text-align: center;
    background-image: linear-gradient(to right top,rgba(193, 218, 82, 0.884),rgb(174, 0, 255),blue);
}
header>i{
  position: absolute;
  left: 5vw;
}
</style>